package com.imooc.demo.config.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.beans.PropertyVetoException;

/**
 * Created by 稻草人 on 2018/6/16.
 * 添加Configuration注解表示获取bean方法，创建一个类似于spring.xml的包含aop和ioc容器的类
 * 添加MapperScan注解表示 配置mybatis mapper的扫描路径
 */
@Configuration
@MapperScan("com.imooc.demo.dao")
public class DataSourceConfiguration {

    //引入application.properties中配置的jdbc参数
    @Value("${jdbc.driver}")
    private String jdbcDriver;
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.username}")
    private String jdbcUsername;
    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Bean(name = "dataSource")
    public ComboPooledDataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass(jdbcDriver);
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setUser(jdbcUsername);
        dataSource.setPassword(jdbcPassword);

        //连接池关闭后是否自动提交
        dataSource.setAutoCommitOnClose(false);
        return dataSource;
    }


}


















